--
-- Created by IntelliJ IDEA.
-- User: yao.chen
-- Date: 2019-01-29
-- 特权礼包（月卡）
-- RewardMonthlyPrivilege.lua
--

local ClassRef = app.mvc.createPanelClass(...)

local IconRender = require("app.widget.renders.IconRender")

function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.RewardMonthlyPrivilege))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end

function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

function ClassRef:init(data)
    self.super.init(self, data)

    local renders = {0, 0, 0}
    for i = 1, #renders do
        local render = IconRender.create(self._ui["Panel/icon_" .. i])
        render:setTipEnable(true)
        render:setTipUnShowOrigin(true)
        renders[i] = render
    end
    self._iconRenders = renders

    self._ui.Button_buy:addTouchEventListener(function(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            local mallModel = app:getInst("MallModel")
            local goods = mallModel:getPayPrivilege()
            if goods and #goods > 0 then
                display.setBtnTouchEnable(self._ui.Button_buy, false)
                mallModel:reqEmaRecharge(self._data.GoodsInfo)
            end
        end
    end)

    self._ui.Button_gift:addTouchEventListener(function(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            app:sendMsg("DailyController", "openView")
            app:sendMsg("RewardController", "closeView")
            app:sendMsg("RewardController", "closePayNoticeView")
        end
    end)

    self._ui.text:setString(L("privilegeduration"))

    self._isSuperPrivilege = false
end

function ClassRef:initItems(drop)
    local items = UD:parseDropIDToBagItem(drop)
    if not items then return end
    local renders = self._iconRenders
    local len = math.min(#items, #renders)
    for i = 1, len do
        renders[i]:setData(items[i])
    end
end

function ClassRef:refresh()
    if not self._data then return end
    local data = self._data
    self:initItems(tonumber(data.Drop))
    if data.SuperPrivilegeDays > 0 then
        self._isSuperPrivilege = true
    else
        self._isSuperPrivilege = false
    end
    self._ui.Button_buy:setEnabled(true)
    self._ui.Button_gift:setEnabled(true)

    if self._isSuperPrivilege then
        self._ui.Button_buy:setVisible(false)
        self._ui.Button_gift:setVisible(true)
        self._ui.tips:setString(string.format(L("privilegelasttime"), data.SuperPrivilegeDays))
    else
        self._ui.Button_buy:setVisible(true)
        self._ui.Button_gift:setVisible(false)

        -- 根据地区转换过的价格
        local mallModel = app:getInst("MallModel")
        local goods = mallModel:getPayPrivilege()
        if goods and #goods > 0 then
            self._ui.tips:setString(string.format(L("privilegeprice"), goods[1].price))
        end
    end
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef
